
#include "CDmLib.h"

//内存读取类
CMemLib::CMemLib()
{
	m_hMutex = CreateMutex(NULL, false, NULL);
}
CMemLib::CMemLib(DWORD pid)
{
	InitProcess(pid);
}
bool CMemLib::InitProcess(DWORD pid)
{
	hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
	hHeap = GetProcessHeap();
	if (!hProcess)
	{
		//创建失败
		return false;
	}
	InitOffSet();
}
CMemLib::~CMemLib()
{
	CloseHandle(hProcess);
	HeapDestroy(hHeap);
}

void  CMemLib::SetOffSetValues(int off1)
{
	WaitForSingleObject(m_hMutex, INFINITE);
	InitOffSet();
	offset[0] = off1;
}
void  CMemLib::SetOffSetValues(int off1, int off2)
{
	WaitForSingleObject(m_hMutex,INFINITE);
	InitOffSet();
	offset[0] = off1;
	offset[1] = off2;
}
void  CMemLib::SetOffSetValues(int off1, int off2, int off3)
{
	WaitForSingleObject(m_hMutex, INFINITE);
	InitOffSet();
	offset[0] = off1;
	offset[1] = off2;
	offset[2] = off3;
}
void  CMemLib::SetOffSetValues(int off1, int off2, int off3, int off4)
{
	WaitForSingleObject(m_hMutex, INFINITE);
	InitOffSet();
	offset[0] = off1;
	offset[1] = off2;
	offset[2] = off3;
	offset[3] = off4;
}
void  CMemLib::SetOffSetValues(int off1, int off2, int off3, int off4,int off5)
{
	WaitForSingleObject(m_hMutex, INFINITE);
	InitOffSet();
	offset[0] = off1;
	offset[1] = off2;
	offset[2] = off3;
	offset[3] = off4;
	offset[4] = off5;
}
void  CMemLib::SetOffSetValues(int off1, int off2, int off3, int off4, int off5,int off6)
{
	WaitForSingleObject(m_hMutex, INFINITE);
	InitOffSet();
	offset[0] = off1;
	offset[1] = off2;
	offset[2] = off3;
	offset[3] = off4;
	offset[4] = off5;
	offset[5] = off6;
}
//重载个取8字节的
bool CMemLib::GetDataFromAddress(DWORD pBase, int num, DWORD64 * word)
{
	int i,  *buff, temp;
	DWORD p = pBase;
	HANDLE process;
	MEMORY_BASIC_INFORMATION mbi;



	for (int i = 0; i < num; i++)
	{

		if (offset[i] == -1)
		{
			break;
		}
		VirtualQueryEx(hProcess, (PVOID)p, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
		if ((mbi.Protect&PAGE_READWRITE) && (mbi.State == MEM_COMMIT))
		{
			if (offset[i+1] == -1)//最后一次取值
			{
				buff = (int *)HeapAlloc(hHeap, NULL, 8); //取8字节节
				//malloc(mbi.RegionSize);

				ReadProcessMemory(hProcess, (LPVOID)(p + offset[i]), buff, 8, NULL);
				//取出偏移里面的内容，再用TEMP做地址，继续查找

				*word = *(DWORD64 *)((BYTE *)buff);
				HeapFree(hHeap, NULL, buff);
				buff = NULL;
			}
			else
			{
				buff = (int *)HeapAlloc(hHeap, NULL, 4);
				//malloc(mbi.RegionSize);

				ReadProcessMemory(hProcess, (LPVOID)(p + offset[i]), buff, 4, NULL);
				//取出偏移里面的内容，再用TEMP做地址，继续查找

				p = *(DWORD *)((BYTE *)buff);
				HeapFree(hHeap, NULL, buff);
				buff = NULL;
			}

		}
	}
	ReleaseMutex(m_hMutex);
	return p;
}
bool  CMemLib::GetDataFromAddress(DWORD pBase, int num, DWORD * word)
{
	int i, p = pBase, *buff, temp;
	HANDLE process;
	MEMORY_BASIC_INFORMATION mbi;
	
	

	for (int i = 0; i < num; i++)
	{
		
		if (offset[i] == -1)
		{
			break;
		}
		VirtualQueryEx(hProcess, (PVOID)p, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
		if ((mbi.Protect&PAGE_READWRITE) && (mbi.State == MEM_COMMIT))
		{
			buff = (int *)HeapAlloc(hHeap, NULL, 4);
			//malloc(mbi.RegionSize);

			ReadProcessMemory(hProcess, (LPVOID)(p + offset[i]), buff, 4, NULL);

			if (offset[i+1] == -1)
			{
				*word = *((DWORD *)buff);
			}
			else
			{
				p = *((DWORD *)buff);
				if (p==0)
				{
					HeapFree(hHeap, NULL, buff);
					buff = NULL;
					ReleaseMutex(m_hMutex); //多线程同步
					return false;
				}
			}

			if (*(int *)((BYTE *)buff) == 0 && pBase == 0x18E3BC)
			{
				OutputDebugStringA("内存值获取出错\n" );
			}
			HeapFree(hHeap, NULL, buff);
			buff = NULL;
		}
		else
		{
			ReleaseMutex(m_hMutex); //多线程同步
			return  false;
		}
	}
	ReleaseMutex(m_hMutex); //多线程同步
	return true;
}
bool CMemLib::GetDataFromAddress(DWORD pBase, int num, WORD * word)
{
	int i, p = pBase, *buff, temp;
	HANDLE process;
	MEMORY_BASIC_INFORMATION mbi;



	for (int i = 0; i < num; i++)
	{

		if (offset[i] == -1)
		{
			break;
		}
		VirtualQueryEx(hProcess, (PVOID)p, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
		if ((mbi.Protect&PAGE_READWRITE) && (mbi.State == MEM_COMMIT))
		{
			if (offset[i + 1] == -1)//最后一次取值
			{
				buff = (int *)HeapAlloc(hHeap, NULL, 2); //取8字节节
														 //malloc(mbi.RegionSize);

				ReadProcessMemory(hProcess, (LPVOID)(p + offset[i]), buff,2, NULL);
				//取出偏移里面的内容，再用TEMP做地址，继续查找

				*word = *(WORD *)((BYTE *)buff);
				HeapFree(hHeap, NULL, buff);
				buff = NULL;
			}
			else
			{
				buff = (int *)HeapAlloc(hHeap, NULL, 4);
				//malloc(mbi.RegionSize);

				ReadProcessMemory(hProcess, (LPVOID)(p + offset[i]), buff, 4, NULL);
				//取出偏移里面的内容，再用TEMP做地址，继续查找
				if (*(int *)((BYTE *)buff) == 0 && i != num - 1)
				{
					HeapFree(hHeap, NULL, buff);
					buff = NULL;
					ReleaseMutex(m_hMutex); //多线程同步
					return false;
				}
				p = *((DWORD *)buff);
				HeapFree(hHeap, NULL, buff);
				buff = NULL;
			}

		}
		else
		{
			ReleaseMutex(m_hMutex); //多线程同步
			return false;
		}
	}
	ReleaseMutex(m_hMutex); //多线程同步
	return true;
}
bool CMemLib::GetDataFromAddress(DWORD pBase, int num, BYTE * word)
{
	int i, p = pBase, *buff, temp;
	HANDLE process;
	MEMORY_BASIC_INFORMATION mbi;



	for (int i = 0; i < num; i++)
	{

		if (offset[i] == -1)
		{
			break;
		}
		VirtualQueryEx(hProcess, (PVOID)p, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
		if ((mbi.Protect&PAGE_READWRITE) && (mbi.State == MEM_COMMIT))
		{
			if (offset[i + 1] == -1)//最后一次取值
			{
				buff = (int *)HeapAlloc(hHeap, NULL, 1); //取8字节节
														 //malloc(mbi.RegionSize);

				ReadProcessMemory(hProcess, (LPVOID)(p + offset[i]), buff, 1, NULL);
				//取出偏移里面的内容，再用TEMP做地址，继续查找

				*word = *((BYTE *)buff);
				HeapFree(hHeap, NULL, buff);
				buff = NULL;
			}
			else
			{
				buff = (int *)HeapAlloc(hHeap, NULL, 4);
				//malloc(mbi.RegionSize);

				ReadProcessMemory(hProcess, (LPVOID)(p + offset[i]), buff, 4, NULL);
				//取出偏移里面的内容，再用TEMP做地址，继续查找
				p = *((DWORD *)buff);
				if (p == 0)
				{
					HeapFree(hHeap, NULL, buff);
					buff = NULL;
					ReleaseMutex(m_hMutex); //多线程同步
					return false;
				}
				HeapFree(hHeap, NULL, buff);
				buff = NULL;
			}

		}
		else
		{
			ReleaseMutex(m_hMutex); //多线程同步
			return false;
		}
	}
	ReleaseMutex(m_hMutex); //多线程同步
	return true;
}
bool CMemLib::GetDataFromAddress(DWORD pBase, int num, char *str,int len)
{
	int i, p = pBase, *buff, temp;
	char *szBuff = NULL;
	HANDLE process;
	MEMORY_BASIC_INFORMATION mbi;
	for (int i = 0; i < num; i++)
	{
		if (offset[i] == -1)
		{
			break;
		}
		VirtualQueryEx(hProcess, (PVOID)p, &mbi, sizeof(MEMORY_BASIC_INFORMATION));
		if ((mbi.Protect&PAGE_READWRITE) && (mbi.State == MEM_COMMIT))
		{
			
			//取出偏移里面的内容，再用TEMP做地址，继续查找
			if (i == num -1) //最后取字符串
			{
				szBuff = (char *)HeapAlloc(hHeap, NULL, len);
				ReadProcessMemory(hProcess, (LPVOID)(p + offset[i]), szBuff, len, NULL);
				memset(str, 0, len);
				if (strlen(szBuff) > 0 && strlen(szBuff) < len)
				{

					//sprintf_s(str, strlen(szBuff), "%s", szBuff);
					memcpy_s(str, strlen(szBuff), szBuff, strlen(szBuff));
				}
				if (!szBuff)
				{
					HeapFree(hHeap, NULL, szBuff);
				}
				
			}
			else
			{
				buff = (int *)HeapAlloc(hHeap, NULL, 4);
				ReadProcessMemory(hProcess, (LPVOID)(p + offset[i]), buff, 4, NULL);
				p = *((DWORD *)buff);
				if (p == 0)
				{
					HeapFree(hHeap, NULL, buff);
					buff = NULL;
					ReleaseMutex(m_hMutex); //多线程同步
					return false;
				}
				HeapFree(hHeap, NULL, buff);
				buff = NULL;
				
			}
			
			
			
		}
		else
		{
			ReleaseMutex(m_hMutex); //多线程同步
			return false;
		}
	}
	ReleaseMutex(m_hMutex); //多线程同步
	return true;
}
void CMemLib::InitOffSet()
{
	for (int i = 0; i < 10; i++)
	{
		offset[i] = -1;
	}
}
